{include file="public/header" /}
<body>
<div class="layui-card">
    <div class="layui-card-body">
        <div class="layui-tab-content">
            <input type="hidden" id="userRole" value="{$user_role_json}">
            <form id="allotRoleForm" class="layui-form layui-form-pane" autocomplete="off">
                <input type="hidden" name="id" value="{$account_info.id}">
                <div class="layui-form-item">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" value="{$account_info.name}" disabled class="layui-input layui-disabled">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">当前账号</label>
                    <div class="layui-input-block">
                        <input type="text" name="account" value="{$account_info.username}" disabled class="layui-input layui-disabled">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">角色</label>
                    <div class="layui-input-block">
                        {volist name="role_list" id="vo"}
                        <input type="checkbox"
                               name="role"
                               title="{$vo.name}"
                               value="{$vo.id}"
                               {in name="$vo.id" value="$user_node"} checked {/in}
                        lay-filter="checkRole"
                        lay-skin="primary">
                        {/volist}
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <!--<button lay-submit lay-filter="sendForm" class="layui-btn layui-btn-normal">提交</button>-->
                        <button type="button" onclick="sendAllotRoleForm('{$account_info.id}')" class="layui-btn layui-btn-normal">提交</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
    var userRole = JSON.parse($('#userRole').val())
    //选中的角色
    var checkRole = [];

    for (let r = 0; r < userRole.length; r++) {
        checkRole.push(userRole[r])
    }

    layui.use('form',function () {
        var $ = layui.jquery,
            form = layui.form;


        form.on('checkbox(checkRole)',function (data) {
            if (data.elem.checked == true) {
                checkRole.push(parseInt(data.value))
            }else {
                var roleIndex = checkRole.indexOf(parseInt(data.value));
                if (roleIndex > -1){
                    checkRole.splice(roleIndex,1);
                }
            }
        });

    });

    function sendAllotRoleForm(id) {
        $.ajax({
            type:"POST",
            url:"{:url('user/allotRole')}",
            data:{
                id:id,
                check_role:checkRole
            },
            dataType:"JSON",
            success:function (res) {
                if (res.code == 'suc') {
                    sucMsg(res.msg,function () {
                        xadmin.close()
                    })
                }else {
                    errMsg(res.msg)
                }
            }
        })
    }
</script>
</body>
</html>